import { parse } from 'url';

//数据字典数据
let tableListDataSource=
[
  {
    id:'0',
    name:'机关',
    property:'单位',
    memo:'系统保留主体，不可编辑'
  },
  {
    id:'1',
    name:'学校',
    property:'单位',
    memo:'系统保留主体，不可编辑'
  },
  {
    id:'2',
    name:'教师',
    property:'人员',
    memo:'系统保留主体，不可编辑'
  },
  {
    id:'3',
    name:'学生',
    property:'人员',
    memo:'系统保留主体，不可编辑'
  },
  {
    id:'4',
    name:'车辆',
    property:'其他项目',
    memo:''
  }
];

//数据字典详情
/**
 * @param
 * 字段类型：文本0 引用1 单选2
 * 是否必填：是1 否0
 * 启用状态：是1 否0
 * 是否可编辑：是1 否0
 */
let tableListDetail=
[
  {
    id:'0',
    name:'机关',
    property:'单位',
    memo:'系统保留主体，不可编辑',
    rows:[
      {
        id:'00',
        name:'教育局',
        type:0,
        required:1,
        status:1,
        editable:0,
        memo:'系统保留字段，不可编辑',
      },
    ]
  },{
    id:'1',
    name:'学校',
    property:'单位',
    memo:'系统保留主体，不可编辑',
    rows:[
      {
        id:'10',
        name:'学校名称',
        type:0,
        required:1,
        status:1,
        editable:0,
        memo:'系统保留字段，不可编辑',
      },
      {
        id:'11',
        name:'机构代码',
        type:0,
        required:1,
        status:1,
        editable:0,
        memo:'系统保留字段，不可编辑',
      },
      {
        id:'12',
        name:'直属机构',
        type:1,
        required:1,
        status:1,
        editable:0,
        memo:'系统保留字段，不可编辑',
      },
      {
        id:'13',
        name:'办学类型',
        type:2,
        required:1,
        status:1,
        editable:1,
        memo:'',
      },
      {
        id:'14',
        name:'学制类型',
        type:2,
        required:1,
        status:1,
        editable:1,
        memo:'',
      },
    ]
  },
  {
    id:'2',
    name:'教师',
    property:'人员',
    memo:'系统保留主体，不可编辑',
    rows:[
      {
        id:'20',
        name:'学校名称',
        type:0,
        required:1,
        status:1,
        editable:0,
        memo:'系统保留字段，不可编辑',
      },
      {
        id:'21',
        name:'机构代码',
        type:0,
        required:1,
        status:1,
        editable:0,
        memo:'系统保留字段，不可编辑',
      },
      {
        id:'22',
        name:'直属机构',
        type:1,
        required:1,
        status:1,
        editable:0,
        memo:'系统保留字段，不可编辑',
      },
      {
        id:'23',
        name:'办学类型',
        type:2,
        required:1,
        status:1,
        editable:1,
        memo:'',
      },
      {
        id:'24',
        name:'学制类型',
        type:2,
        required:1,
        status:1,
        editable:1,
        memo:'',
      },
    ]
  },
  {
    id:'3',
    name:'学生',
    property:'人员',
    memo:'系统保留主体，不可编辑',
    rows:[
      {
        id:'30',
        name:'学生姓名',
        type:1,
        required:1,
        status:1,
        editable:0,
        memo:'系统保留字段，不可编辑',
      },
      {
        id:'31',
        name:'学籍号',
        type:0,
        required:1,
        status:1,
        editable:0,
        memo:'系统保留字段，不可编辑',
      },
      {
        id:'32',
        name:'联系电话',
        type:1,
        required:1,
        status:1,
        editable:0,
        memo:'系统保留字段，不可编辑',
      },
      {
        id:'33',
        name:'班级',
        type:2,
        required:1,
        status:1,
        editable:1,
        memo:'',
      },
      {
        id:'34',
        name:'学制类型',
        type:2,
        required:1,
        status:1,
        editable:1,
        memo:'',
      },
    ]
  },
  {
    id:'4',
    name:'车辆',
    property:'其他项目',
    memo:'',
    rows:[
      {
        id:'40',
        name:'车辆名称',
        type:0,
        required:1,
        status:1,
        editable:0,
        memo:'系统保留字段，不可编辑',
      },
      {
        id:'41',
        name:'车辆代码',
        type:0,
        required:1,
        status:1,
        editable:0,
        memo:'系统保留字段，不可编辑',
      }
    ]
  }
];


export function getSysDic(req, res, u) {
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }

  const params = parse(url, true).query;

  let dataSource = [...tableListDataSource];

  if (params.sorter) {
    const s = params.sorter.split('_');
    dataSource = dataSource.sort((prev, next) => {
      if (s[1] === 'descend') {
        return next[s[0]] - prev[s[0]];
      }
      return prev[s[0]] - next[s[0]];
    });
  }

  if (params.status) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
      filterDataSource = filterDataSource.concat(
        [...dataSource].filter(data => parseInt(data.status, 10) === parseInt(s[0], 10))
      );
    });
    dataSource = filterDataSource;
  }

  if (params.no) {
    dataSource = dataSource.filter(data => data.no.indexOf(params.no) > -1);
  }

  let pageSize = 10;
  if (params.pageSize) {
    pageSize = params.pageSize * 1;
  }

  const result = {
    list: dataSource,
    pagination: {
      total: dataSource.length,
      pageSize,
      current: parseInt(params.currentPage, 10) || 1,
    },
  };

  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }
}

export function postSysDic(req, res, u, b) {
  let url = u;

  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }

  const body = (b && b.body) || req.body;
  const { method, id,name,property,memo } = body;

  switch (method) {
    /* eslint no-case-declarations:0 */
    case 'delete':
      tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);
      break;
    case 'post'://新增
      tableListDataSource.unshift({
        id:id,
        name:name,
        property:property,
        memo:memo
      });
      break;
    default:
      break;
  }

  const result = {
    list: tableListDataSource,
    pagination: {
      total: tableListDataSource.length,
    },
  };

  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }
}

//获取数据字典详细信息
export function getSysDicDetail(req, res, u) {
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }

  const params = parse(url, true).query;

  let dataSource = [...tableListDetail];//深拷贝


  if (params.id) {
    dataSource = dataSource.filter(data => data.id.indexOf(params.id) > -1);
  }

  let pageSize = 10;
  if (params.pageSize) {
    pageSize = params.pageSize * 1;
  }

  const result = {
    list: dataSource[0].rows,
    pagination: {
      total: dataSource[0].rows.length,
      pageSize,
      current: parseInt(params.currentPage, 10) || 1,
    },
  };

  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }
}

export function postSysDicDetail(req, res, u, b) {
  let url = u;

  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }

  const body = (b && b.body) || req.body;
  const { method, pid,data} = body;
  let result = {
    success:false,
    id:null,
  };
  switch (method) {
    /* eslint no-case-declarations:0 */
    // case 'delete':
    //   tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);
    //   break;
    case 'post'://新增
     //找到符合条件的对象数据filter返回新数组，向其对象的数组里增加数据
      let tableListDetailadd = tableListDetail.filter(item => item.id===pid);//pid.indexOf(item.id) > -1);
      if(tableListDetailadd.length){
        tableListDetailadd[0].rows.unshift({
          ...data
        });
        result.success = true;
        result.id = pid;
      }
      break;
    case 'patch'://修改
        let tableListDetailupdate = tableListDetail.filter(item => item.id===pid);
        let isExist = false
        tableListDetailupdate[0].rows = tableListDetailupdate[0].rows.map(item=>{
          if(item.id ===data.id){
            isExist = true;
            return Object.assign({},item,data)
          }
          return item;
        })
        if(isExist){
          result.success = true;
          result.id = pid;
        }
       break;
    default:
      break;
  }



  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }
}



export default {
  getSysDic,
  postSysDic,
  getSysDicDetail,
  postSysDicDetail
};
